package org.legzo.castorranker.web;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import org.apache.wicket.ajax.AjaxRequestTarget;
import org.apache.wicket.ajax.markup.html.AjaxLink;
import org.apache.wicket.markup.html.WebPage;
import org.apache.wicket.markup.html.basic.Label;
import org.apache.wicket.markup.html.form.Button;
import org.apache.wicket.markup.html.form.Form;
import org.apache.wicket.markup.html.form.TextField;
import org.apache.wicket.markup.html.list.ListItem;
import org.apache.wicket.markup.html.list.PropertyListView;
import org.legzo.castorranker.RankerService;
import org.legzo.castorranker.export.ChartMaker;
import org.legzo.castorranker.ranker.Game;
import org.legzo.castorranker.ranker.Player;

public class AdminPage extends WebPage {

	private PlayerForm playerForm;

	public AdminPage() {

		RankerService rankerService = RankerApplication.get().getRankerService();

		List<Player> players = rankerService.getPlayers();
		List<PlayerDto> playerDtos = new ArrayList<PlayerDto>();
		for (Player player : players) {
			playerDtos.add(new PlayerDto(player));
		}

		playerForm = new PlayerForm("playersForm", playerDtos);
		add(playerForm);
	}

	private class PlayerForm extends Form<List<PlayerDto>> {

		private static final long serialVersionUID = 7425839771260279626L;

		private List<PlayerDto> players;

		public PlayerForm(String id, final List<PlayerDto> givenPlayers) {
			super(id);
			this.players = givenPlayers;

			PropertyListView<PlayerDto> playersList = buildPlayerList();
			add(playersList);

			AjaxLink<HomePage> addPlayerLink = buildAddPlayerLink();
			add(addPlayerLink);

			Button submitButton = buildSubmitButton();
			add(submitButton);
		}

		private Button buildSubmitButton() {
			Button submitButton = new Button("submitButton") {
				private static final long serialVersionUID = 6274325566106703263L;

				@Override
				public void onSubmit() {
					ChartMaker chartMaker = new ChartMaker();
					RankerService rankerService = RankerApplication.get().getRankerService();
					Game newGame = new Game(new Date());

					for (PlayerDto playerDto : players) {
						newGame.addPlayer(playerDto.getPlayer(), playerDto.getScore());
					}

					rankerService.saveGame(newGame);

					List<Player> loadedPlayers = rankerService.getPlayers();
					chartMaker.printChart(loadedPlayers, System.getProperty("java.io.tmpdir") + "/ranker/scores.png");

				}
			};
			return submitButton;
		}

		private PropertyListView<PlayerDto> buildPlayerList() {
			PropertyListView<PlayerDto> playersList = new PropertyListView<PlayerDto>("playersList", players) {
				private static final long serialVersionUID = 4289069821176726588L;

				@Override
				protected void populateItem(final ListItem<PlayerDto> item) {
					item.add(new TextField<String>("name"));
					item.add(new Label("ranking"));
					item.add(new TextField<String>("score"));
				}
			};

			playersList.setOutputMarkupId(true);
			return playersList;
		}

		private AjaxLink<HomePage> buildAddPlayerLink() {
			AjaxLink<HomePage> addPlayerLink = new AjaxLink<HomePage>("addPlayerLink") {
				private static final long serialVersionUID = -4385903292339938261L;

				@Override
				public void onClick(AjaxRequestTarget target) {
					players.add(new PlayerDto(new Player("unknown")));
					target.addComponent(playerForm);
				}
			};
			return addPlayerLink;
		}
	}
}
